package com.cj.sys.web.security;

import org.springframework.beans.factory.annotation.Autowired;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;
import com.cj.sys.entity.security.SysConfig;
import com.cj.sys.service.security.SysConfigManager;

public class SysConfigAction extends CRUDActionSupport<SysConfig>{
	@Autowired
	SysConfigManager sysConfigManager;

	/**
	 * <p>【系统变量同步】</p>
	 *
	 * @return
	 */
	public String synchronize(){
		try {
			sysConfigManager.synSystemConfig();
			Struts2Utils.renderText("系统变量同步成功!");
		} catch (Exception e) {
			setErrMessage(e.getMessage());
			Struts2Utils.renderText("error:" + getErrMessage());
		}
		return null;
	}
	
	/**
	 * <p>【变量同步】</p>
	 *
	 * @return
	 */
	public String synchronizeType(){
		try {
			String type = Struts2Utils.getParameter("filter_LIKE_type");
			if(type == null || type.equalsIgnoreCase("")){
				Struts2Utils.renderText("请选择类型");
				return null;
			}
			if(SysConfig.SYSTEM_PROPERTY.equalsIgnoreCase(type))
				return synchronize();
			String result = sysConfigManager.sysConfigByType(type);
			Struts2Utils.renderText(result);
		} catch (Exception e) {
			setErrMessage(e.getMessage());
			Struts2Utils.renderText("error:" + getErrMessage());
		}
		return null;
	}
	
	@Override
	public EntityManager getEntityManager() {
		return sysConfigManager;
	}
	
	protected void prepareModel() throws Exception{
		if (getId() != null) {
			entity = sysConfigManager.get(getId());
		} else {
			entity = new SysConfig();
		}
	}

}
